layui.use(['table', 'element', 'form', 'laydate', "code"], function () {
});

var linux;
var log;
$(function () {
    // 初始化代码编辑器
    linux = CodeMirror.fromTextArea(document.getElementById('exec-linux'), {lineNumbers: true, mode: 'xml'});
    var operateType = getCookie("exec-shell-type");
    var flinkServiceName = getCookie("exec-shell-name");
    switch(operateType) {
        case "startNoSavePoint":
            $.get("/job/startNoSavePoint?jobName=" + flinkServiceName, function (data) {
                linux.setValue(data);
            });
            break;
        case "startWithSavePoint":
            var filePath = getCookie("exec-shell-path");
            $.get("/job/startWithSavePoint?jobName=" + flinkServiceName + "&filePath=" + filePath, function (data) {
                linux.setValue(data);
            });
            break;
        case "stopWithSavePoint":
            $.get("/job/stopWithSavePoint?jobName=" + flinkServiceName, function (data) {
                linux.setValue(data);
            });
            break;
        default:
            linux.setValue('');
    }

    // 初始化执行日志编辑器
    log = CodeMirror.fromTextArea(document.getElementById('exec-log'), {lineNumbers: true, mode: 'xml'});
    log.setValue('');
    CodeMirror.colorize();

});


/**
 * 执行命令
 */
function exec_linux() {
    layer.confirm('确认执行吗？', {btn: ['确定','取消']}, function(){
        layer.close(layer.index);

        var id = getCookie("exec-shell-id");
        var type = null;
        var operateType = getCookie("exec-shell-type");
        switch(operateType) {
            case "startNoSavePoint":
                type = 3;
                break;
            case "startWithSavePoint":
                type = 4;
                break;
            case "stopWithSavePoint":
                type = 5;
                break;
        }

        var load = layer.load(1, {shade: [0.1,'#000']});
        $.ajax({
            type: "POST",
            url: "/job/execute",
            contentType: "application/json",
            data: JSON.stringify({
                id: id,
                type: type,
                command: linux.getValue(),
            }),
            success: function (req) {
                log.setValue(req);
                layer.close(load);
            }
        });
    }, function(){
    });
}
